<?php

if (!defined('IN_SYS'))
    exit('Access Denied');

class jsc_controller extends admincp {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $lists = jsc::get_title_dir();
        krsort($lists);
        require self::tpl('jsc');
    }

    public function content() {
        $lists = jsc::get_content_dir();
        krsort($lists);
        require self::tpl('jsc_content');
    }

    public function contentimport() {
        $lists = jsc::get_content_dir();
        krsort($lists);
        require self::tpl('jsc_contentimport');
    }

    public function titleimport() {
        require self::tpl("jsc_titleimport");
    }

    //导入词库
    public function title_upload() {
        $new_path = ROOT . '/jscdata/keys/';
        $this->p_upload($new_path);
    }

    //导入源文件
    public function content_upload() {
        $new_path = ROOT . '/jscdata/contents/';
        $this->p_upload($new_path);
    }
    
    //公共上传
    public function p_upload($new_path) {
        $time = time();
        $config = array(
            "pathFormat" => "/data/temp/{time}{rand:6}",
            "maxSize" => 1024 * 1024 * 1024 * 100,
            "allowFiles" => array('.txt'),
        );
        $up = new uploader('file', $config, 'upload');
        $data = $up->getFileInfo();
        if (empty($data['url'])) {
            $this->respone_json($data['state']);
        }
        $file = ROOT . $data['url'];
        $new_name = md5_file($file);
        if (!is_dir($new_path)) {
            mkdir($new_path, 0777, true);
        }
        if (is_file($new_path . $new_name . '.txt')) {
            $this->respone_json('文件已存在');
        }
        $content = file_get_contents($file);
        unlink($file);
        $encode = mb_detect_encoding($content, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'CP936'));
        $content = mb_convert_encoding($content, 'UTF-8', $encode);
        $bool = file_put_contents($new_path . $new_name . '.txt', chr(0xEF) . chr(0xBB) . chr(0xBF) . $content);
        if ($bool) {
            unlink($file);
            $this->respone_json(['suc' => 1]);
        } else {
            $this->respone_json('操作失败');
        }
    }

    
    public function del() {
        $tm_id = gp('id');
        $type = gp('type');
        if (empty($tm_id)) {
            self::json(['suc' => 0]);
        }
        if (is_string($tm_id)) {
            $ids[] = $tm_id;
        } elseif (is_array($tm_id)) {
            $ids = $tm_id;
        }
        $pass = gp('pass');
        $userid = $this->user['id'];
        $rs = db::find('select * from `admin` where id=' . $userid);
        if (empty($pass) || admin::check_passwd($rs, $pass) == false) {
            self::json(['suc' => 0, 'message' => '管理员密码不正确']);
        }
        foreach ($ids as $id) {
            $name = pathinfo($id);
            if ($type == 'content') {
                $files = ROOT . '/jscdata/contents/' . $name['filename'] . '.txt';
            } else {
                $files = ROOT . '/jscdata/keys/' . $name['filename'] . '.txt';
            }

            if (is_file($files)) {
                unlink($files);
            }
        }
        self::json(['suc' => 1]);
    }

}
